import Vue from 'vue';
import {
    baseUrl, //引入baseUrl
} from "../config/env";
import App from './App.vue';
import router from './router';
import area from './utils/area';
import store from './store';
import "lib-flexible/flexible";
import "assets/iconfont/iconfont.css";
import "assets/css/flex.css";
import FastClick from 'fastclick';
// import  http from '../config/axios';
import axios from 'axios'
import VueAxios from 'vue-axios'
import Qs from 'qs';
import api from './api/index'
import VueTouch from 'vue-touch'
import { Lazyload } from 'vant';
import { Popup } from 'vant'
import {
    Swipe, SwipeItem, Toast, Icon, Cell
    , Button, Tab, Tabs, Grid, GridItem, CellGroup
    , Dialog, Form, Field, Uploader, NumberKeyboard
    , Sticky, Overlay, Calendar, IndexBar, IndexAnchor
    , Search, Tabbar, PullRefresh, List, Loading, NoticeBar
    , Checkbox, CheckboxGroup
} from 'vant';

Vue.use(Swipe).use(SwipeItem).use(Toast).use(Icon).use(Cell)
    .use(Button).use(Tab).use(Tabs).use(Grid).use(GridItem)
    .use(CellGroup).use(Dialog).use(Form).use(Field).use(Uploader)
    .use(NumberKeyboard).use(Sticky).use(Overlay).use(Calendar)
    .use(IndexBar).use(IndexAnchor).use(Search).use(Tabbar)
    .use(PullRefresh).use(List).use(Loading).use(NoticeBar)
    .use(Checkbox).use(CheckboxGroup);


Vue.use(Popup);
Vue.use(area);
Vue.use(Lazyload);
Vue.use(VueAxios, axios);
Vue.use(Qs);
// Vue.use(http);
Vue.use(VueTouch, { name: 'v-touch' });
Toast.setDefaultOptions({ duration: 1000 });

Vue.prototype.$Qs = Qs;
// Vue.prototype.$http = http;
Vue.prototype.$axios = axios;
Vue.prototype.$index = api.index.index;
Vue.prototype.$api = api;
Vue.config.productionTip = false;
VueTouch.config.swipe = {
    threshold: 100 //手指左右滑动距离
};


const str = navigator.userAgent.toLowerCase();
const ver = str.match(/cpu iphone os (.*?) like mac os/);

if (!ver) {//非IOS系统
    // 引入fastclick 做相关处理
    FastClick.attach(document.body);
}
else {
    if (parseInt(ver[1]) < 11) {
        // 引入fastclick 做相关处理
        FastClick.attach(document.body);
    }
}
new Vue({
    router,//$route  ,$router
    store, // $store
    render: h => h(App)
}).$mount('#app');

axios.default.baseURL = baseUrl

axios.interceptors.request.use(config => {
    if (store.state.token) {
        config.headers.common['token'] = store.state.token;
    }
    return config;  //添加这一行
});
axios.interceptors.response.use(res => {
    //对响应数据做些事
    if (res.data.code == 404) {
        // alert(res.data.msg);
        localStorage.removeItem('token_user');
        location.href = api.index.index + '/home';
        return Promise.reject(res)
    }
    return res
}, error => {
    if (error.response.status === 401) {
        // 401 说明 token 验证失败
        // 可以直接跳转到登录页面，重新登录获取 token
        location.href = api.index.index + '/home';
    } else if (error.response.status === 500) {
        // 服务器错误
        // do something
        return Promise.reject(error.response.data)
    }
    // 返回 response 里的错误信息
    return Promise.reject(error.response.data)
});

//路由守卫

